package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company")
@CrossOrigin
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    // 保存企业
    @PostMapping("")
    public Result save(@RequestBody Company company) {
        companyService.add(company);
        return new Result(ResultCode.SUCCESS);
    }

    // 保存企业
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") String id, @RequestBody Company company) {
        company.setId(id);
        companyService.update(company);
        return new Result(ResultCode.SUCCESS);
    }

    // 删除id
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        companyService.delete(id);
        return new Result(ResultCode.SUCCESS);
    }

    // 查询by id
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) throws Exception {
        throw new CommonException(ResultCode.UNAUTHORISE);
        /*Company company =  companyService.findById(id);
        return new Result(ResultCode.SUCCESS, company);*/
    }

    // 查询列表
    @GetMapping("")
    public Result findAll() {
        List<Company> company =  companyService.findAll();
        int i = 1/0;
        return new Result(ResultCode.SUCCESS, company);
    }
}
